<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <a href="#aaa">1</a>
  <a href="#bbb">2</a>
  <a href="#ccc">3</a>
  <a href="#ddd">4</a>

  <script>
    window.onhashchange = function () {
      console.log(location.hash)
    }
  </script>
</body>

</html>